package developer.核心类.线程;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author zhangyongkang
 * @time 2025/8/14 11:38
 * @description
 */
public class AQSTest {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2, () -> {
            System.out.println("end");
        });

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                System.out.println("到达屏障");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("冲破屏障");
            }).start();
        }

        new Thread(() -> {
            System.out.println("last到达屏障");
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
            System.out.println("last冲破屏障");
        }).start();

    }
}
